//url:
//baseurl:   关于动态数据   根据传入string进行判断 并返回
//search:   search 值，封装函数
//hash ：      hash值，并且传入函数进行hash值变化进行操作
//hashlist: 获取全部的hash值，并作对象处理
//maxlen：   最大长度，传入true/false   是否最后加三个点
var common = {
	url: function(){
		return 'http://cjrh.csdp.edu.cn'
	},
	localurl: function(){
		return 'http://127.0.0.1:8020'
	},
	timechange: function(arr,onoff){
		var times = '';
		var timed = arr;
		var timearr = timed.split(' ')[0].split('-');
		if(onoff){
			times = timearr.join('/')
		}else{
			times = timearr;
		}
		return times;
	},
	Interface: function(val){
		var arr=[];
		if(val == 'news'){
			arr = ['/api/news/list.json?type=FRONT',
				'/api/news/list.json?type=LOCAL',
				'/api/news/list.json?type=COLLEGE',
				'/api/news/list.json?type=EXTRATERRITORIAL'
			]
		}else if(val == 'transform'){
			arr = [
				'/api/transform/list.json?type=COMPREHENSIVE',
				'/api/transform/list.json?type=ENGINEERING',
				'/api/transform/list.json?type=PEDAGOGICAL',
				'/api/transform/list.json?type=FINANCE',
				'/api/transform/list.json?type=MEDICINE',
				'/api/transform/list.json?type=ARTS'
			]
		}else if(val == 'result'){
			arr = [
				'/api/result/list.json?type=REFORM',
				'/api/result/list.json?type=INNOVATION',
				'/api/result/list.json?type=TRAINING',
				'/api/result/list.json?type=DEVELOPMENT'
			]
		}else if(val == 'anli'){
			arr = [
				'/api/anli/list.json?type=TRAINING',
				'/api/anli/list.json?type=INNOVATION',
				'/api/anli/list.json?type=SERVICE',
				'/api/anli/list.json?type=COOPERATION'
			]
		}else if(val == 'policy'){
			arr = [
				'/api/policy/list.json?type=NATIONAL',
				'/api/policy/list.json?type=PROVINCIAL',
				'/api/policy/list.json?type=SCHOOL',
				'/api/policy/list.json?type=OTHER'
			]
		}else if(val =='detail'){
			arr = ['/api/article/info.json?articleId=']
		} else if(val == 'register'){
			arr = ['/api/register.json?']
		} else if(val == 'login'){
			arr = ['/api/login.json?']
		} else if(val == 'lostpas'){
			arr = ['/api/forgotPassword.json?']
		} else if(val == 'proj1'){
			//工程概况
			arr = ['/api/school/list.json',
				'/api/school/list.json?category=1',
				'/api/school/list.json?category=2',
				'/api/school/list.json?category=3',
				'/api/school/list.json?category=4',
				'/api/school/list.json?category=5',
				'/api/school/list.json?category=6'
			]
		} else if(val == 'proj2'){
			//招标发布
			arr = ['/api/project/tender.json']
		} else if(val == 'proj3'){
			//工程进展
			arr = [
			    [
			     	'/api/province/all.json'
			    ],
			    [
			     	'/api/project/progress.json?category=0',
			     	'/api/project/progress.json?category=1',
			     	'/api/project/progress.json?category=2',
			     	'/api/project/progress.json?category=3',
			     	'/api/project/progress.json?category=4',
			     	'/api/project/progress.json?category=5',
			     	'/api/project/progress.json?category=6'
			    ],
			    [
			     	'/api/province/all.json'
			    ],
			    [
			     	'/api/province/school.json?provinceId=',
			     	'/api/province/info.json?provinceId='
			    ]
			]
		} else if(val == 'proj4'){
			//完成项目
			arr = ['/api/project/over.json']
		} else if(val == 'school'){
			//学校详情
			arr = ['/api/school/info.json?schoolId=']
		} else if(val == 'schoollist'){
			//学校项目详情
			arr = ['/api/project/news.json?projectId=']
		} else if(val == 'onlogin'){
			// 是否登陆
			arr = ['/api/isLogin.json']
		} else if(val == 'changepas'){
			arr = ['/api/user/modifyPassword.json?']
		} else if(val == 'index'){
			arr = ['/api/index.json']
		} else if(val == 'lostlogin'){
			arr = ['/api/logout.json']
		}

		return arr
	},
	baseval: function(val){
		var arr = [];
		if(val == 'news'){
			arr = ['前沿要闻','地方新声','工程动态','域外传真'];
		}else if(val == 'transform'){
			arr = ['综合类院校','工科类院校','师范类院校','财经管理类院校','农林医药类院校','艺术类院校']
		}else if(val == 'project'){
			arr = [
				{
					title:'工程概况',
					child:['全部','综合性院校','工科类院校','师范类院校','财经管理类院校','农林医药类院校','艺术类院校']
				},
				{
					title:'校企合作信息',
					child:[]
				},
				{
					title:'项目进展',
					child:['工程总沙盘','百校进展','各省进展']
				},
				{
					title:'完成项目',
					child:[]
				}
			]
		}else if(val == 'result'){
			arr = ['教育改革成果','科研创新成果','人才培养成果','教师发展成果'];
		}else if(val == 'anli'){
			arr = ['人才培养','科研创新','社会服务','国际合作'];
		}else if(val == 'policy'){
			arr = ['国家及部委政策','行业及省市政策','学校政策','其他文件'];
		}else if(val == 'about'){
			arr = ['联系我们','关于我们'];
		}
		return arr;
	},
	search: function(){
		var arr = {};
		var arr1 = window.location.search.substring(1).split('&');
		for(var i=0;i<arr1.length;i++){
			var arr2 = arr1[i].split('=');
			arr[arr2[0]] = arr2[1];
		}; 
		return arr;
	},
	hash: function(obj,fn){
		var arr = common.hashlist();
		if(arr[obj]){
			var hash = arr[obj];
		}else{
			var hash = 0;
		}
		
		hashChangeFire();
		if( ("onhashchange" in window) && ((typeof document.documentMode==="undefined") || document.documentMode==8)) {  
		    // 浏览器支持onhashchange事件  
		    window.onhashchange = hashChangeFire;  // TODO，对应新的hash执行的操作函数  
		} else {  
		    // 不支持则用定时器检测的办法  
		    setInterval(function() {  
		    	var hash2 = window.location.hash.substr(1);
		        var ischanged = isHashChanged(hash2);  // TODO，检测hash值或其中某一段是否更改的函数  
		        if(ischanged) {  
		            hashChangeFire();  // TODO，对应新的hash执行的操作函数  
		        }  
		    }, 150);  
		}  
		function hashChangeFire(){
			var arr = common.hashlist();
			var hash2 = arr[obj];
			if(!hash2){
				hash2 = 0;
			}
			hash = hash2;
			fn(hash);
		};
		function isHashChanged(hash2){
			var on = false;
			if(hash != hash2){
				on = true;
			}
			return on;
		};
	},
	hashlist:function(){
		var arr = {};
		var arr1 = window.location.hash.substring(1).split('&');
		for(var i=0;i<arr1.length;i++){
			var arr2 = arr1[i].split('=');
			arr[arr2[0]] = arr2[1];
		}; 
		return arr;
	},
	maxlen: function(arr,len,onoff){
		if(arr.length>=len){
			if(onoff){
				arr=arr.substring(0,len)+"...";
			}else{
				arr=arr.substring(0,len);
			}
		}
		return arr
	},
	pagesgn: function(pages,showpage){
		var pages = pages;
		var showpage = showpage;
		var npage = 1;
		if(common.hashlist().page){
			npage = common.hashlist().page;
		}
		
		autopage();
		function autopage(){
			$('.allpage').html(pages).attr({'href':'#page='+ pages});
			var start=0;
			var end=0;
			//判断条件来展示效果
			if(pages<=showpage){
				start=1;
				end=pages;
			}else{
				showpage = 4;
				var offsetRight= Math.floor(showpage / 2);
				var offsetLeft = showpage - offsetRight - 1;
				start = npage - offsetLeft;
				if (start < 1) {
					start = 1;
				}
				end = start + showpage - 1;
				if (end > pages) {
					end = pages;
					start = end - showpage + 1;
				}	
			}
			$('.pages-box ul').html('');
			for(var i=start;i<=end;i++){
				var ali=document.createElement('li');
				ali.style.cursor="pointer";
				ali.innerHTML=i;
				if(pages<=showpage){
					$('.abouthide2').css({'display':'none'});
					$('.abouthide1').css({'display':'none'});
				}else{
					if(pages/2 >= npage){
						$('.abouthide2').css({'display':'block'});
						$('.abouthide1').css({'display':'none'});
					}else{
						$('.abouthide2').css({'display':'none'});
						$('.abouthide1').css({'display':'block'});
					}
				}
				if(i==npage){
					ali.className="hov5"
				}
				ali.onclick=function(){
					npage=this.innerHTML;
					location.hash = '#page='+ npage;
					autopage();
				}
				$('.pages-box ul').append(ali)
			}
		}
		//上一页
		$('.pages-prev').on('click',function(){
				npage--;
				if(npage<=1){
					npage=1;
				}
				location.hash = '#page='+ npage;
				autopage();
		})
		//下一页
		$('.pages-next').on('click',function(){
				npage++;
				if(npage>=pages){
					npage=pages
				}
				location.hash = '#page='+ npage;
				autopage();
		})
		$('.first-pag').on('click',function(){
			npage = 1;
			location.hash = '#page='+ npage;
			autopage();
		})
		$('.allpage').on('click',function(){
			npage = pages;
			location.hash = '#page='+ npage;
			autopage();
		})
	}
}


$(function(){	
	// 关于推荐文章
	var tuijian = 0;
	$('.tuijian-titlebox span').bind('click',function(){
		tuijian++;
		if(tuijian == 1){
			$(this).addClass('tuijian-bg');
			$('.tuijian-list').hide()
		}else{
			$(this).removeClass('tuijian-bg');
			tuijian = 0;
			$('.tuijian-list').show()
		}
	})
})
window.onload=function(){
	//推荐文章的内容
	ser('/api/article/recommend.json',function(data){
		var html = '';
		var json = data.data;
		if(json.length>0){
			for(var i=0;i<json.length;i++){
				if(i<3){
					var title = common.maxlen(json[i].title,22,true);
					var times = common.timechange(json[i].pubdate,true);
					if(json[i].minorType == 'PROJECT'){
						html+='<li><a href="/schoollist.htm?schxmid='+ json[i].id +'" class="clear">\
							<div class="tjimg-box fl">\
								<img src="'+ json[i].picture +'" alt="" /></div>\
							<div class="tj-valbox fr">\
								<p class="tj-title">'+ title +'</p>\
								<div class="see-time clear">\
									<p class="tj-time fl">'+ times +'</p>\
									<p class="tj-see fl">'+ json[i].readCount +'</p>\
								</div></div></a></li>';
					}else{
						html+='<li><a href="/detail.htm?id='+ json[i].id +'" class="clear">\
							<div class="tjimg-box fl">\
								<img src="'+ json[i].picture +'" alt="" /></div>\
							<div class="tj-valbox fr">\
								<p class="tj-title">'+ title +'</p>\
								<div class="see-time clear">\
									<p class="tj-time fl">'+ times +'</p>\
									<p class="tj-see fl">'+ json[i].readCount +'</p>\
								</div></div></a></li>';
					}
				}
			}
			$('.tuijian-list').html(html)
		}
	})
	
	
	
	//获取省份
	ser('/api/province/all.json',function(data){
		if(data.data.length>0){
			var sfhtml = '<option value="">-- 省份 --</option>';
			for(var i=0;i<data.data.length;i++){
				sfhtml+='<option value="'+ data.data[i].id +'">'+ data.data[i].name +'</option>'
			}
			$('select[name=provinceId]').html(sfhtml);
		}
	});
	$('select[name=provinceId]').change(function(){
		ser('/api/province/school.json?provinceId='+this.value,function(data){
			var schtml = '<option value="">-- 学校 --</option>';
			for(var i=0;i<data.data.length;i++){
				schtml+='<option value="'+ data.data[i].id +'">'+ data.data[i].name +'</option>'
			}
			$('select[name=schoolId]').html(schtml);
		})
	});
	//sousuo kasihi
	$('.search-box .search-btn').bind('click',function(){
		//panduan checkbox
		var onok = true;
			$('.outsel').each(function(i,e){
				if(e.value == ''){
					onok = false;
					alert('省份和学校为必选项，请选择...');
					return false;
				}
			});
			
			
			if(onok){
				if($('input[name=PROJECT]').is(':checked') && !$('input[name=TENDER]').is(':checked') && !$('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT'
				}else if(!$('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && !$('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=TENDER'
				}else if(!$('input[name=PROJECT]').is(':checked') && !$('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=NEWS'
				} else if($('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && !$('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT&types=TENDER'
				} else if($('input[name=PROJECT]').is(':checked') && !$('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT&types=NEWS'
				} else if(!$('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=TENDER&types=NEWS'
				} else if($('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT&types=TENDER&types=NEWS'
				}else{
					alert('请勾选查询项...')
					return false;
				}
				if($('.keyword').val() != '' || $('.keyword').val()){
					window.location.href=ukl;
				}else{
					alert('请输入查找关键字');
					return false;
				}
			}else{
			}
		
	})
	
	function ser(jk,fn){
		$.ajax({
			url : jk,
			type: "POST",
			error: function(request) {
				console.log(request)
			},
			dataType:"json",
			success: function(data) {
				if(!data.ret) return;
				fn?fn(data):'';
			}
		})
	}
}